package com.pb.wkflow.core.listener;

import com.pb.infra.oth.util.SpringContextUtil;
import com.pb.wkflow.core.service.WorkflowService;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.delegate.DelegateTask;
import org.camunda.bpm.engine.delegate.TaskListener;
import org.camunda.bpm.engine.impl.persistence.entity.TaskEntity;
import org.camunda.bpm.model.bpmn.instance.MultiInstanceLoopCharacteristics;

import java.util.Collection;

/**
 * 任务开始监听器
 * @author 苗益辉
 * @date 2022/4/26 17:22
 */
@Slf4j
public class TaskEventListener implements TaskListener {

    @Override
    public void notify(DelegateTask delegateTask) {
        WorkflowService workflowService = SpringContextUtil.getBean(WorkflowService.class);
        log.info("------------ TaskEventListener 任务开始监听 ------------");
        log.info("TaskEventListener 当前节点：{}，当前实例id：{}",
                delegateTask.getId(),delegateTask.getProcessInstanceId());
        Collection<MultiInstanceLoopCharacteristics> mls = delegateTask.getBpmnModelElementInstance()
                .getChildElementsByType(MultiInstanceLoopCharacteristics.class);
        if(mls.isEmpty()) {
            // 不是会签
            workflowService.addParticipantUser((TaskEntity) delegateTask);
        }
    }
}
